home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9146 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need psudeo code for binary search
  5. Date: Wed, 06 Mar 96 21:41:51 GMT
  6. Organization: none
  7. Message-ID: <826148511snz@genesis.demon.co.uk>
  8. References: <Pine.SOL.3.91.960229154211.27358B-100000@obelix> <4h77mu$qtb@news1.cle.ab.com> <313877A6.4047@ix.netcom.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <313877A6.4047@ix.netcom.com>
  15.            nbullen@ix.netcom.com "Norman Bullen" writes:
  16.  
  17. >Donald-Anthony C. Phillips wrote:
  18. >> 
  19. >> The binary search algorithm works as follows:
  20. >> 1) Remember the structure must already be sorted.
  21. >> 2) It works better as a recursive function 
  22. >It actually works better when written as a loop because it will use much 
  23. >less stack space.
  24. >
  25. >int BinarySearch(char *pstrTarget, char *pstrTable[], int tableSize)
  26. >{  int comp, hi, lo, look;
  27. >
  28. >   hi = tableSize-1; lo = 0;
  29. >   while (hi>=lo) {
  30. >      look = (hi+lo)/2;
  31. >      comp = strcmp(pstrTarget, pstrTable[look]);
  32. >      if (comp==0) return look; /* Found it! */
  33. >      if (comp>0)
  34. >         lo = look+1;
  35. >      else
  36. >         hi = look-1;
  37. >                  }
  38. >   return 0; /* Didn't find it. */
  39. >}
  40.  
  41. >(You may have to play with that piece of code a little; I think its right 
  42. >but I did not attempt any sort of testing.)
  43.  
  44. It looks apart from the last return statement however it can be marginally
  45. simplified. When you have a range that consists of an even number of elements
  46. you select an element that splits the rest into two subranges. Since you have
  47. an odd number of elements left the size of the 2 subranges differ by 1. The
  48. code above always rounds down therefore making the lower range smaller. You
  49. can instead make the upper range smaller by changing:
  50.  
  51.        look = (hi+lo)/2;
  52.  
  53. to
  54.  
  55.        look = (hi+lo+1)/2;
  56.  
  57. This is interesting because the function as a whole can then be simplified
  58. to:
  59.  
  60. size_t BinarySearch(char *pstrTarget, char *pstrTable[], size_t tableSize)
  61. {  int comp;
  62.    size_t hi, lo, look;
  63.  
  64.    hi = tableSize; lo = 0;
  65.    while (hi>lo) {
  66.       look = (hi+lo)/2;
  67.       comp = strcmp(pstrTarget, pstrTable[look]);
  68.       if (comp==0) return look; /* Found it! */
  69.       if (comp>0)
  70.          lo = look+1;
  71.       else
  72.          hi = look;
  73.    }
  74.    return -1; /* Didn't find it. */
  75. }
  76.  
  77. I've changed the size parameter and the index variables to size_t for
  78. generality since it will then work for any sized array that the
  79. implementation can support (well, nearly, given the following).
  80.  
  81. 0 is a valid success code and would indicate a match on the first element
  82. of the array so I've changed this to -1. The value actually returned
  83. is (size_t)-1 and since size_t is an unsigned type this corresponds to the
  84. largest value that size_t can represent. It can be tested against (size_t)-1
  85. in the caller function.
  86.  
  87. -- 
  88. -----------------------------------------
  89. Lawrence Kirby | fred@genesis.demon.co.uk
  90. Wilts, England | 70734.126@compuserve.com
  91. -----------------------------------------
  92.